package cn.krui.project.controller.system;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.krui.helper.bean.AppResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @program: project-system
 * @description: 工具模块
 * @author: zc
 * @create: 2020-3-13
 */
@Api(tags = "管理-工具", description = "工具:上传")
@RestController
@RequestMapping("/system/tools")
@CrossOrigin
public class ToolsController extends SystemBaseController {

    @Value("${com.kr.web.common.filePath}")
    private String upfilePath;

    @Value("${com.kr.web.common.urlroot}")
    private String urlroot;

    @Value("${com.kr.web.common.urlupload}")
    private String urlupload;

	@ApiOperation(value = "文件上传", httpMethod = "POST")
    @RequestMapping(value = "/fileUp", method = RequestMethod.POST)
    @ResponseBody
    public AppResult<Map<String, Object>> fileUploadOne(@RequestParam("file") MultipartFile file) {
        AppResult<Map<String, Object>> app = new AppResult<Map<String, Object>>();
        Map<String,Object> out = new HashMap<>(3);
        // 获得原始文件名+格式
        String fileName = file.getOriginalFilename();
        // 截取文件名
        String fname = fileName.substring(0, fileName.lastIndexOf("."));
        // 截取文件格式
        String format = fileName.substring(fileName.lastIndexOf(".") + 1);
        // 获取当前时间(精确到毫秒)
        long ms = System.currentTimeMillis();
        String timeMs = String.valueOf(ms);
        // 原文件名+当前时间戳作为新文件名
        String videoName = fname + "_" + timeMs + "." + format;
        out.put("videoname", videoName);
        String filelocalPath = "";
        Date date = new Date();
        String dateOne = new SimpleDateFormat("yyyy/MM/dd/").format(date);
        filelocalPath = upfilePath + "/" + dateOne;
        out.put("videopath",  "/" +dateOne + videoName);
        out.put("videofullpath",  urlroot + "/" +dateOne + videoName);
        File f = new File(filelocalPath);
        if (!f.exists()){
            f.mkdirs();
        }
        if (!file.isEmpty()) {
            try {
                FileOutputStream fos = new FileOutputStream(filelocalPath + videoName);
                InputStream in = file.getInputStream();
                // InputStream in = request.getInputStream();
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = in.read(bytes)) != -1) {
                    fos.write(bytes, 0, len);
                }
                fos.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        app.ok();
        app.setDataSingle(out);
        return app;
    }
    
	@ApiOperation(value = "获取文件上传信息", httpMethod = "POST")
    @RequestMapping(value = "/fileUpInfo", method = RequestMethod.POST)
    @ResponseBody
    public AppResult<Map<String, Object>> getFileUploadInfo(){
        AppResult<Map<String, Object>> app = new AppResult<Map<String, Object>>();
        Map<String,Object> out = new HashMap<>(2);
        out.put("urlupload", urlupload);
        out.put("urlroot", urlroot);
        app.ok();
        app.setDataSingle(out);
        return app;
    }

}